<template>
  <div class="game_list_container">
    <p class="title">充值产品</p>
    <div class="tabs_container">
      <div
        class="item noselect"
        :class="activeIndex === index ? 'active' : ''"
        @click="tabChange(index)"
        v-for="(item, index) in tabs"
        :key="index"
      >
        {{ item }}
      </div>
    </div>

    <div class="content_list">
      <div class="img_wrap" @click="handleAdd(item)" v-for="(item, index) in imgList" :key="index">
        <img :src="item.img" alt="" />
      </div>
    </div>

    <PayDialog :visible="visible" :item="gameItem" @close="visible = false" />
  </div>
</template>

<script>
import { reactive, toRefs, onMounted, nextTick } from 'vue';
import PayDialog from './payDialog';
export default {
  name: 'GameList',
  components: {
    PayDialog,
  },
  setup() {
    const state = reactive({
      tabs: ['热门', '端游', '手游', '其他'],
      activeIndex: 0,
      imgList: [],
      gameItem: {},
      visible: false,
    });

    onMounted(() => {
      setImgList(1, 15, [
        '梦幻西游中脑版',
        '武魂',
        '魔法禁书目录',
        '泰亚史诗',
        '天下',
        'CC',
        '大话西游2',
        '秘境对决',
        '镇魔曲',
        '非人学院',
        '飞刀',
        '天下',
        '梦幻西游',
        '率土之滨',
      ]);
    });

    const tabChange = index => {
      state.activeIndex = index;
      let start;
      let end;
      let name = [];
      switch (index) {
        case 0:
          start = 1;
          end = 15;
          name = [
            '梦幻西游中脑版',
            '武魂',
            '魔法禁书目录',
            '泰亚史诗',
            '天下',
            'CC',
            '大话西游2',
            '秘境对决',
            '镇魔曲',
            '非人学院',
            '飞刀',
            '天下',
            '梦幻西游',
            '率土之滨',
          ];
          break;
        case 1:
          start = 15;
          end = 20;
          name = ['天唐无双', '有杀气童话', '精灵传说', '泰亚史诗', '飞飞', '元气战姬学院'];
          break;
        case 2:
          start = 20;
          end = 29;
          name = [
            '龙魂时刻',
            '三国如龙传',
            '大话西游2',
            '天谕',
            '天下3',
            '逆水寒',
            '乱斗西游2',
            '倩女幽魂',
            '新倩女幽魂',
          ];
          break;
        case 3:
          start = 29;
          end = 31;
          name = ['大话西游', '新大话3'];
          break;
      }
      setImgList(start, end, name);
    };

    const setImgList = (start, end, names = []) => {
      state.imgList = [];
      for (let i = start; i < end; i++) {
        state.imgList.push({ name: names[i], img: require(`@/assets/game_list/${i}.jpg`) });
      }
    };

    const handleAdd = item => {
      state.gameItem = item;
      nextTick(() => {
        state.visible = true;
      });
    };

    return {
      ...toRefs(state),
      tabChange,
      handleAdd,
    };
  },
};
</script>

<style lang="scss" scoped>
.game_list_container {
  .title {
    margin: 20px 0;
    font-size: 24px;
    font-weight: 700;
    color: #d21b35;
  }
  .tabs_container {
    display: flex;
    align-items: center;
    line-height: 50px;
    background-color: #d3d1d1;
    font-size: 20px;
    .item {
      width: calc(100% / 4);
      text-align: center;
      cursor: pointer;
      border-right: 1px solid #ffffff;
      &.active {
        background-color: #e05555;
        color: #ffffff;
      }
    }
  }

  .content_list {
    height: 600px;
    overflow-y: scroll;
    display: flex;
    flex-wrap: wrap;
    scrollbar-width: 50px;

    .img_wrap {
      position: relative;
      box-sizing: border-box;
      width: calc(100% / 4);
      height: 160px;
      overflow: hidden;
      cursor: pointer;
      img {
        width: 100%;
        height: 100%;
        border: none;
        transform: scale(1);
        transition: transform 0.5s;
      }
      &:hover {
        &::after {
          display: block;
          position: absolute;
          top: 0;
          bottom: 0;
          left: 0;
          right: 0;
          content: '';
          background-color: rgba(0, 0, 0, 0.2);
        }
        img {
          transform: scale(1.2);
          transition: transform 0.5s;
        }
      }
    }
  }
}
</style>
